package com.guosheng.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.guosheng.dao.SysRoleMapper;
import com.guosheng.dao.SysUserMapper;
import com.guosheng.dao.SysUserRoleMapper;
import com.guosheng.pojo.ActivePassword;
import com.guosheng.pojo.ActiveUser;
import com.guosheng.pojo.ItemsCustom;
import com.guosheng.pojo.ItemsQueryVo;
import com.guosheng.pojo.SysRole;
import com.guosheng.pojo.SysRoleExample;
import com.guosheng.pojo.SysUser;
import com.guosheng.pojo.SysUserExample;
import com.guosheng.pojo.SysUserRole;
import com.guosheng.pojo.SysUserRoleExample;
import com.guosheng.service.SysUserService;

import net.sf.json.JSONObject;

@Controller
//定义url的根路径，访问时根路径+方法的url
@RequestMapping("/user")
public class UserController {
  @Autowired
   SysUserService sysUserService;
  @Autowired
  SysUserRoleMapper sysUserRoleMapper;
  @Autowired
 SysRoleMapper  sysRoleMapper;
  @Autowired
 SysUserMapper sysUserMapper;

  @RequestMapping("/manager")
  public ModelAndView manager(){
	  //查询用户的信息
	  ModelAndView m=new ModelAndView();
	List<SysUser>list=  sysUserService.selectAllUser();
	  m.addObject("list", list);
	  m.setViewName("/user/UserList");
	  return m;
  }
  @RequestMapping("addUserView")
  public String addUserView(){
	  return "/user/addUser";
  }
  @RequestMapping("addUser")
  public ModelAndView addUser(SysUser user) throws Exception{
	  //新增一个用户之后返回道用户列表
	  //查询道角色id
	  SysRoleExample sysExample = new SysRoleExample();
	  SysRoleExample.Criteria rolecriteria = sysExample.createCriteria();
	  rolecriteria.andNameEqualTo(user.getRoleName());
	 List<SysRole> list= sysRoleMapper.selectByExample(sysExample);
	 //将用户角色id设置
	 user.setRole_id(list.get(0).getId());
	 boolean flag= sysUserService.insertUser(user);
//	 if(flag){
//	 return "redirect:/user/manager";
//	 }else{
//		 return null;
//	 }
	return  manager();
  }
  @RequestMapping("updateUserView")
  public ModelAndView updateView(ActiveUser user){
	  ModelAndView m=new ModelAndView();
	m.addObject("activeUser", user);
	 m.setViewName("/user/updateUser"); 
	  return m;
  }
  @RequestMapping("updateUser")
  @ResponseBody
  public JSONObject updateUser(ActivePassword activePassword) throws Exception{
		 JSONObject json=new JSONObject();

	  if(StringUtils.isNotEmpty(activePassword.getPasswordnew1())
			  &&StringUtils.isNotEmpty(activePassword.getPasswordnew2())
			  &&StringUtils.isNotEmpty(activePassword.getPreviousPassword())){
		  if(activePassword.getPasswordnew1().
				  equals(activePassword.getPasswordnew2())){
		  SysUser user=new SysUser();
		  user.setUsercode(activePassword.getUsercode());
			  //查询道角色id
			 boolean flag= sysUserService.updateUserPassword
					 (activePassword.getPreviousPassword(),
							 activePassword.getPasswordnew1(),user);
			 if(flag){
				 json.put("resultMsg", "更新成功");
			 }else{
				 json.put("resultMsg", "原密码错误");
			 }
		  }else{
			  json.put("resultMsg", "两次输入不一致") ;
		  }
	  }else{
		 json.put("resultMsg", "输入不能为空") ;
		 }
 
	 return json;
  

}
  @RequestMapping("deleteUser")
  public ModelAndView deleteUser(SysUser user){
	boolean flag=  sysUserService.deleteUser(user.getId());
//	if(flag){
//		return "redirect:/user/manager";
//	}else{
//	  return null;
//  }
	return manager();
  }
}